﻿using System.Collections.Generic;
using HPPUtil;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HPP.UnitTest
{
    
    
    /// <summary>
    ///这是 BitArrayTest 的测试类，旨在
    ///包含所有 BitArrayTest 单元测试
    ///</summary>
    [TestClass()]
    public class BitArrayTest
    {
        
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        
       
        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion
        
       

        #region Set

        /// <summary>
        /// Set 的测试
        /// 
        /// 输入：符合范围内的位置
        /// 
        /// 输出：对应位置被正确置位
        ///</summary>
        /// <remarks>
        /// 2009-08-23
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_Set_when_pos_in_legal_range()
        {
            int len = 31; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值
            
            target.Set(2, true);
            CollectionAssert.AreEqual(new byte[]{ 0x0, 0x0, 0x0, 0x2 }, target.Array);

            target.Set(31, true);
            CollectionAssert.AreEqual(new byte[] { 64, 0x0, 0x0, 0x2 }, target.Array);

            target.Set(31, false);
            CollectionAssert.AreEqual(new byte[] { 0x0, 0x0, 0x0, 0x2 }, target.Array);
        }

        /// <summary>
        /// Set 的测试
        /// 
        /// 输入：不符合范围内的位置
        /// 
        /// 预期输出：Array属性值不变
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_Set_when_pos_in_illegal_range()
        {
            int len = 31; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值

            target.Set(2, true);

            byte[] nowBytes = target.Array;

            target.Set(99, true);

            CollectionAssert.AreEqual(nowBytes, target.Array);

        }

        #endregion

        #region GetHasBlocks

        /// <summary>
        /// GetHasBlocks 的测试
        /// 
        /// 输入：
        /// 
        /// 预期输出：与Array对应的blockNum列表
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetHasBlocks()
        {
            int len = 31; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值

            target.Set(2, true).Set(99, true).Set(31, true).Set(31, false).Set(30, true);

            CollectionAssert.AreEquivalent(new List<int>() { 2, 30 }, target.GetHasBlocks());
        }

        #endregion

        #region IsHasBlock

        /// <summary>
        /// IsHasBlock 的测试
        /// 
        /// 输入：blockNum
        /// 
        /// 预期输出：正确的是否拥有信息
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_IsHasBlock()
        {
            int len = 31; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值

            target.Set(2, true).Set(99, true).Set(31, true).Set(31, false).Set(30, true);

            Assert.AreEqual(true, target.IsHasBlock(2));
            Assert.AreEqual(false, target.IsHasBlock(3));
            Assert.AreEqual(false, target.IsHasBlock(99));
        }

        #endregion

        #region GetHexString

        /// <summary>
        /// GetHexString 的测试
        /// 
        /// 输入：
        /// 
        /// 预期输出：正确的十六进制字符串
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetHexString()
        {
            int len = 31; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值

            target.Set(2, true).Set(99, true).Set(31, true).Set(31, false).Set(30, true).Set(16, true);

            Assert.AreEqual("20008002", target.GetHexString());
        }

        /// <summary>
        /// GetHexString 的测试
        /// 
        /// 输入：blockCount为1，全部为0的BitArray
        /// 
        /// 预期输出：正确的十六进制字符串
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetHexString_when_blockCount_equals_1_and_all_blocks_are_0()
        {
            int blockCount = 1; // TODO: 初始化为适当的值
            BitArray target = new BitArray(blockCount); // TODO: 初始化为适当的值

            

            Assert.AreEqual("0", target.GetHexString());
        }

        /// <summary>
        /// GetHexString 的测试
        /// 
        /// 输入：有前置0的BitArray
        /// 
        /// 预期输出：不含前置0的HexString
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetHexString_when_has_prefix_zero()
        {
            int len = 32; // TODO: 初始化为适当的值
            BitArray target = new BitArray(len); // TODO: 初始化为适当的值

            target.Set(4, true).Set(8, true).Set(12, true).Set(16, true);

            Assert.AreEqual("00008888", target.GetHexString());
        }

        #endregion

        #region GetFromHexString

        /// <summary>
        /// GetFromHexString 的测试
        /// 
        /// 输入：十六进制字符串及对应的blockCount
        /// 
        /// 预期输出：原来的十六进制字符串
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetFromHexString_when_two_parameter_are_consistent()
        {
            BitArray bitArray = BitArray.GetFromHexString("20008002", 31);
            Assert.IsTrue(bitArray.IsHasBlock(2));
            Assert.IsTrue(bitArray.IsHasBlock(16));
            Assert.IsTrue(bitArray.IsHasBlock(30));
            Assert.AreEqual(31, bitArray.BlockCount);
            Assert.AreEqual("20008002", bitArray.GetHexString()); 
        }

        /// <summary>
        /// GetFromHexString 的测试
        /// 
        /// 输入：blockCount小于十六进制字符串所需Array最短长度
        /// 
        /// 预期输出：null
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetFromHexString_when_blockCount_is_less_than_minimum_lenght_of_array_of_hexstring()
        {
            BitArray bitArray = BitArray.GetFromHexString("20008002", 28);
            Assert.IsNull(bitArray);
        }

        /// <summary>
        /// GetFromHexString 的测试
        /// 
        /// 输入：blockCount大于十六进制字符串所需Array最长长度
        /// 
        /// 预期输出：null
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetFromHexString_when_blockCount_is_more_than_max_lenght_of_array_of_hexstring()
        {
            BitArray bitArray = BitArray.GetFromHexString("20008002", 33);
            Assert.IsNull(bitArray);
        }

/*
        /// <summary>
        /// GetFromHexString 的测试
        /// 
        /// 输入：原来BitArray有前置0的HexString, 比HexString.Length * 4大的 blockCount
        /// 
        /// 
        /// 预期输出：原来的十六进制字符串
        ///</summary>
        /// <remarks>
        /// vls
        /// </remarks>
        [TestMethod()]
        public void Test_GetFromHexString_when_original_bitArray_has_prefix_zero_and_corresponding_blockCount()
        {
            BitArray bitArray = BitArray.GetFromHexString("8888", 32);
            Assert.IsNotNull(bitArray);
            Assert.IsTrue(bitArray.IsHasBlock(4));
            Assert.IsTrue(bitArray.IsHasBlock(8));
            Assert.IsTrue(bitArray.IsHasBlock(12));
            Assert.IsTrue(bitArray.IsHasBlock(16));
            Assert.AreEqual(32, bitArray.BlockCount);
            Assert.AreEqual("8888", bitArray.GetHexString());
        }
*/

        #endregion

        #region GetDownLoadBlockNum

        /// <summary>
        /// 输入的两个BitArray为空的测试用例
        ///
        /// 输入：空的otherBitArray，设置0的blockCount
        /// 
        /// 输出：空的List
        /// 
        ///</summary>
        ///
        ///<remarks>
        ///xuzhuol
        ///</remarks>
        ///
        [TestMethod()]
        public void Test_GetDownLoadBlockNumTest_when_Both_BitArray_is_empty()
        {
            int blockCount = 0; // TODO: 初始化为适当的值
            BitArray target = new BitArray(blockCount); // TODO: 初始化为适当的值
            BitArray otherBitArray = null; // TODO: 初始化为适当的值
            List<int> expected = new List<int>() { };// TODO: 初始化为适当的值
            List<int> actual;
            actual = target.GetDownLoadBlockNum(otherBitArray);
            CollectionAssert.AreEqual(expected, actual);
        }

        /// <summary>
        /// 输入仅otherBitArray为空的测试用例
        ///
        /// 输入：空的otherBitArray，设置非0的blockCount
        /// 
        /// 输出：空的List
        /// 
        ///</summary>
        ///
        ///<remarks>
        ///xuzhuol
        ///</remarks>
        ///
        [TestMethod()]
        public void Test_GetDownLoadBlockNumTest_when_Only_otherBitArray_is_empty()
        {
            int blockCount = 9; // TODO: 初始化为适当的值
            BitArray target = new BitArray(blockCount); // TODO: 初始化为适当的值
            BitArray otherBitArray = null; // TODO: 初始化为适当的值
            List<int> expected = new List<int>() { };// TODO: 初始化为适当的值
            List<int> actual;
            actual = target.GetDownLoadBlockNum(otherBitArray);
            CollectionAssert.AreEqual(expected, actual);
        }

        /// <summary>
        /// 输入正常的测试用例
        ///
        /// 输入：
        /// otherBitArray：111
        /// this.BitArray：010
        /// 
        /// 输出：101，{1，3}
        /// 
        ///</summary>
        ///
        ///<remarks>
        ///xuzhuol
        ///</remarks>
        ///
        [TestMethod()]
        public void Test_GetDownLoadBlockNumTest_when_None_is_empty()
        {
            int i;
            int blockCount = 3; // TODO: 初始化为适当的值
            BitArray target = new BitArray(blockCount); 
            BitArray otherBitArray = new BitArray(blockCount);
            for (i = 1; i <= otherBitArray.BlockCount; i++)
                otherBitArray.Set(i, true);
            target.Set(2, true);

            List<int> expected = new List<int>(){1,3};
            List<int> actual;
            actual = target.GetDownLoadBlockNum(otherBitArray);
            CollectionAssert.AreEqual(expected, actual);
        }


        #endregion
        
        
    }
}
